<?php
declare (strict_types = 1);

namespace fortuna;
use PHPMailer\PHPMailer\PHPMailer;
class Email
{
    /**
     * 验证找回链接的sercet时效性
     *
     * @param [type] $sercet
     * @return boolval
     */
    public function verify_forgetpwd_sercet($sercet){
        return true;
    }
    /**
     * 发送找回密码的链接
     *
     * @param [type] $to
     * @param [type] $username
     * @param [type] $url
     * @return void
     */
    public function sendForgetPwd($to,$username,$url){
        if(!$this->check_email_format($to) || empty($username || empty($url)) ){
            return false;
        }

        $mail = new PHPMailer(true);    // PHPMailer邮件实例
        $mail->isSMTP();
        $mail->Host = 'smtp.163.com';
        $mail->SMTPAuth = true;
        $mail->CharSet = 'utf-8';
        $mail->Username = 'makeup1124@163.com';
        $mail->Password = 'cnj9mf5d';
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 25;
        $mail->setFrom('makeup1124@163.com', 'Fortuna');
        $mail->addAddress($to, '用户['.$username.']');     // Add a recipient
        // $mail->addAddress('ellen@example.com');               // Name is optional
        // $mail->addReplyTo('info@example.com', 'Information');
        // $mail->addCC('cc@example.com');
        // $mail->addBCC('bcc@example.com');
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = '['.$username.']重置密码操作';
        $mail->Body    = '<p>请您尽快点击如下链接进行密码重置操作：</p><p><a href="'.$url.'" target="_blank">点击重置</a>！</p>';
        $mail->AltBody = '请您尽快点击如下链接进行密码重置操作：'.$url;
        
        if(!$mail->send()){
            echo $mail->ErrorInfo;
        } else {
            return true;
        }
    }
    /**
     * 验证Email格式
     *
     * @param [type] $email_str
     * @return bool
     */
    public function check_email_format($email_str) :bool{
        return true;
    }
}